package com.wj.infrastructure.mapper;

import com.wj.domain.entity.ChapterEntity;
import com.wj.domain.param.BookParam;
import com.wj.domain.vo.BookVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @description 小说Mapper
 * @author wj
 * @date 2022-05-11
 */
@Mapper
public interface BookMapper {

    @Results(value = {
        @Result(property = "id", column = "id"),
        @Result(property = "bookName", column = "book_name"),
        @Result(property = "bookUrl", column = "book_url"),
        @Result(property = "author", column = "author"),
        @Result(property = "type", column = "type"),
        @Result(property = "newest", column = "newest"),
        @Result(property = "state", column = "state"),
        @Result(property = "number", column = "number"),
        @Result(property = "bookDesc", column = "book_desc"),
        @Result(property = "imgUrl", column = "img_url"),
        @Result(property = "views", column = "views"),
        @Result(property = "cacheStatus", column = "cache_status"),
        @Result(property = "updateTime", column = "update_time"),
    })
    @Select(value = "<script>" +
            " select a.id,a.book_name,a.book_url,a.author,a.type," +
            " a.newest,a.state,a.number,a.book_desc,a.img_url,a.views, a.cache_status," +
            " date_format(a.update_time,'%Y-%m-%d') as update_time " +
            " from book as a " +
            " where a.deleted=0 " +
            "<if test='param.bookName != null and param.bookName.length > 0'>" +
            " and a.book_name like concat('%',#{param.bookName},'%')" +
            "</if>" +
            "<if test='param.type != null and param.type != 0'>" +
            " and a.type = #{param.type}" +
            "</if>" +
            " order by a.create_time desc " +
       "</script>")
    List<BookVO> getList(@Param("param") BookParam param);

    @Results(value = {
        @Result(property = "id", column = "id"),
        @Result(property = "bookName", column = "book_name"),
        @Result(property = "bookUrl", column = "book_url"),
        @Result(property = "author", column = "author"),
        @Result(property = "type", column = "type"),
        @Result(property = "newest", column = "newest"),
        @Result(property = "state", column = "state"),
        @Result(property = "number", column = "number"),
        @Result(property = "bookDesc", column = "book_desc"),
        @Result(property = "imgUrl", column = "img_url"),
        @Result(property = "views", column = "views"),
        @Result(property = "cacheStatus", column = "cache_status"),
        @Result(property = "updateTime", column = "update_time"),
    })
    @Select(value = "<script>" +
            " select a.id,a.book_name,a.book_url,a.author,a.type," +
            " a.newest,a.state,a.number,a.book_desc,a.img_url,a.views, a.cache_status," +
            " date_format(a.update_time,'%Y-%m-%d') as update_time " +
            " from book as a where a.id=#{id}" +
            "</script>")
    BookVO getById(@Param("id") Long id);

    @Results(value = {
            @Result(property = "id", column = "id"),
            @Result(property = "bookName", column = "book_name"),
            @Result(property = "author", column = "author"),
            @Result(property = "type", column = "type"),
            @Result(property = "newest", column = "newest"),
            @Result(property = "state", column = "state"),
            @Result(property = "number", column = "number"),
            @Result(property = "bookDesc", column = "book_desc"),
            @Result(property = "imgUrl", column = "img_url"),
            @Result(property = "bookUrl", column = "book_url"),
            @Result(property = "views", column = "views"),
            @Result(property = "updateTime", column = "update_time"),
    })
    @Select("<script>" +
            "select id,book_name,author,type," +
            " newest,state,number,book_desc,img_url,book_url,views " +
            " from book where book_url =#{bookUrl}" +
            "</script>")
    List<BookVO> queryByUrl(@Param("bookUrl") String bookUrl);

    @Select("<script>" +
            " select id,book_name as bookName, book_desc bookDesc, book_url bookUrl from book where id in " +
            " (select book_id as bookId from chapter " +
            " where content_id not in (select id from chapter_content) group by book_id )")
    List<BookVO> getNoChapterContentBook();
}